--#include "data\config\activityconfig\CombineServer\CombineServerConfig.lua" once
--合服首次沙比克攻城战


--合服活动的占领行会
function SendCombineServerGuildSiegeData(sysarg)
    local guildName = ""
	local rankPtr = Ranking.getRanking(RankList[RANK_CS_GUILDSBK_WAR].strName)
	if rankPtr then
		local rankCount = Ranking.getRankItemCount(rankPtr)
		if rankCount > 0 then
			local item = Ranking.getItemFromIndex( rankPtr,0 )
			if item then
				guildName = Ranking.getSub(item, 0)
			end
		end
	end
    --print("guildName ="..guildName)
	if not guildName then
		guildName = ""
	end
	local npack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCombineServerGetGuildSiegeData)
	if npack  then
		DataPack.writeString(npack,guildName)
		DataPack.flush(npack)
	end
end


--合服活动的沙城战
function OnCombineServerGuildSiegeOverHandle()
	local day = System.getDaysSinceCombineServer()
	if day <= 0 then
		return
	end
	local GuildSiegCfg = CombineServerCfg.GuildSiege
	if day < GuildSiegCfg.day[1]  then
		return
	end
	local guildName = ""
	local rankPtr = Ranking.getRanking(RankList[RANK_CS_GUILDSBK_WAR].strName)
	if rankPtr then
		local rankCount = Ranking.getRankItemCount(rankPtr)
		if rankCount > 0 then
			local item = Ranking.getItemFromIndex( rankPtr,0 )
			if item then
				guildName = Ranking.getSub(item, 0)
			end
		end
	end
	--print("guildName ="..guildName)
	if guildName and guildName ~= "" then
		return 
	end

	--攻城占领行会
	local nCityOwnGuildId 	= System.getSbkGuildId()
	local pGuild 			= Guild.getGuild(nCityOwnGuildId)
	if nCityOwnGuildId > 0 and pGuild then
		local guildName 	= Guild.getGuildName(nCityOwnGuildId)
		local memberList 	= Guild.getGuildMemberIdList(pGuild) or {}
		local leaderId 		= Guild.getGuildLeaderId(nCityOwnGuildId)
		local title 		= Lang.ScriptTips.CombineServer008
		local content1 		= Lang.ScriptTips.CombineServer009
		local content2 		= Lang.ScriptTips.CombineServer010
		local logId 		= LogIds.CombineServerSbkWar
		local logStr 		= Lang.LogTips.CombineServerFirstSeigeWar

		for k,v in pairs(memberList) do
			local sex,job = Guild.getGuildMemberBasicData(pGuild, v, 0, 0)	--获取此成员的sex和job（成员可能不在线）
			if v == leaderId then --首领
				local awards  = FilterCommonAwardsByCond(GuildSiegCfg.leaderAwards, sex, job )     --过滤职业性别
				if #awards ~= 0 then
					Awards.GiveToMailById(v, awards, title, content1, logId, logStr)
				end
			else
				local awards  = FilterCommonAwardsByCond(GuildSiegCfg.memberAwards, sex, job )     --过滤职业性别
				if #awards ~= 0 then
					Awards.GiveToMailById(v, awards, title, content2, logId, logStr)
				end
			end
		end

		if guildName then
			gRankMgr_SetRank(nCityOwnGuildId, RANK_CS_GUILDSBK_WAR, 0,  guildName)
			local msg = string.format(Lang.ScriptTips.CombineServer011, guildName)
			System.broadcastTipmsg(msg, ttChatWindow)
		end
	end
end